<?php

namespace app\libs\data;

abstract class Form
{

    public static function __callStatic($template, $args = [])
    {
        if(method_exists(get_called_class(), $template))
            return forward_static_call_array($template, $args);
        else
        {
            if(isset($args[0]))
            {
                $action = $args[0];
                $values = isset($args[1]) ? $args[1] : [];
                return static::generate($action, $template, $values);
            }
        }

        return null;
    }

    protected static function template($template, array $vars = [])
    {
        $path = dirname(__FILE__) . '/forms/' . $template . '.php';
        if(file_exists($path))
        {
            $file = file_get_contents($path);
            foreach($vars as $name => $value)
                $file = str_replace('{' . $name . '}', $value, $file);

            return $file;
        }

        return null;
    }

    public static function generate($files = [], array $vars = [])
    {
        $html = '';
        foreach((array)$files as $file)
            $html .= static::template($file);

        return $html;
    }

}
